package com.doctcloud.hisinfo.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doctcloud.common.core.utils.StringUtils;
import com.doctcloud.hisinfo.common.mapper.PriceListDoMapper;
import com.doctcloud.hisinfo.common.request.PriceListParam;
import com.doctcloud.hisinfo.common.service.IPriceListService;
import com.doctcloud.hisinfo.histable.domain.comm.PriceListDo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

import static com.doctcloud.common.core.constant.QueryConstants.REQ_PARAM;

@Service
@Slf4j
public class PriceListService extends ServiceImpl<PriceListDoMapper, PriceListDo> implements IPriceListService {
    @Override
    public PriceListDo getPriceListPointByDateTime(PriceListParam param) {
        // 统一参数校验
        if (!validateParam(param)) {
            return null;
        }

        LambdaQueryWrapper<PriceListDo> query = Wrappers.lambdaQuery();
        query.eq(PriceListDo::getItemClass, param.getItemClass())
                .eq(PriceListDo::getItemCode, param.getItemCode())
                .eq(PriceListDo::getItemSpec, param.getItemSpec())
                .le(PriceListDo::getStartDate, param.getPointTime())
                .orderByDesc(PriceListDo::getStartDate);

        // 使用通用分页方式，只查询1条记录，不统计总数
        IPage<PriceListDo> page = new Page<>(1, 1, false);
        IPage<PriceListDo> resultPage = this.page(page, query);

        List<PriceListDo> records = resultPage.getRecords();
        return CollectionUtils.isEmpty(records) ? null : records.get(0);
    }

    @Override
    public List<PriceListDo> listPriceListByItem(PriceListParam param) {
        // 统一参数校验
        if (!validateParam(param)) {
            return null;
        }

        LambdaQueryWrapper<PriceListDo> query = Wrappers.lambdaQuery();
        query.eq(PriceListDo::getItemClass, param.getItemClass())
                .eq(PriceListDo::getItemCode, param.getItemCode())
                .eq(PriceListDo::getItemSpec, param.getItemSpec())
                .orderByAsc(PriceListDo::getStartDate);

        return baseMapper.selectList(query);
    }

    @Override
    public List<PriceListDo> listPriceListAfterDate(Date startDate) {
        if (ObjectUtils.isEmpty(startDate)) {
            log.error("日期不能为空");
        }
        LambdaQueryWrapper<PriceListDo> query = Wrappers.lambdaQuery();
        query.ge(PriceListDo::getStartDate,startDate);
        return baseMapper.selectList(query);
    }

    @Override
    public List<PriceListDo> listPriceListBeforeDate(Date startDate) {
        if (ObjectUtils.isEmpty(startDate)) {
            log.error("日期不能为空");
        }
        LambdaQueryWrapper<PriceListDo> query = Wrappers.lambdaQuery();
        query.ge(PriceListDo::getStartDate,startDate);
        return baseMapper.selectList(query);
    }

    @Override
    public PriceListDo getPriceListLast(PriceListParam param) {
        // 统一参数校验
        if (!validateParam(param)) {
            return null;
        }

        LambdaQueryWrapper<PriceListDo> query = Wrappers.lambdaQuery();
        query.eq(PriceListDo::getItemClass, param.getItemClass())
                .eq(PriceListDo::getItemCode, param.getItemCode())
                .eq(PriceListDo::getItemSpec, param.getItemSpec())
                .orderByDesc(PriceListDo::getStartDate); // 按开始日期降序

        // 使用分页查询，高效获取最后一条记录
        IPage<PriceListDo> page = new Page<>(1, 1, false);
        IPage<PriceListDo> resultPage = this.page(page, query);

        List<PriceListDo> records = resultPage.getRecords();
        return CollectionUtils.isEmpty(records) ? null : records.get(0);
    }

    @Override
    public List<PriceListDo> dblistAll() {
        LambdaQueryWrapper<PriceListDo> query = new LambdaQueryWrapper<>();
        query.orderByAsc(PriceListDo::getStartDate);
        return baseMapper.selectList(query);
    }

    /**
     * 统一参数校验方法
     * @param param 查询参数
     * @return true if 参数有效, false otherwise
     */
    private boolean validateParam(PriceListParam param) {
        if (param == null) {
            log.error("查询参数PriceListParam不能为空");
            return false;
        }
        if (StringUtils.isEmpty(param.getItemClass())){
            log.error("{}项目类别，查询无效", REQ_PARAM);
            return false;
        }
        if (StringUtils.isEmpty(param.getItemCode())){
            log.error("{}项目代码，查询无效", REQ_PARAM);
            return false;
        }
        if (StringUtils.isEmpty(param.getItemSpec())){
            log.error("{}项目规格，查询无效", REQ_PARAM);
            return false;
        }
        return true;
    }

}
